package com.example.mywiseelves.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.example.mywiseelves.bean.ElvesBean;
import com.example.mywiseelves.bean.UserBean;
import com.example.mywiseelves.utils.DBHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ZJK
 * @data 2021 19
 * @description 对精灵表的数据进行增删查改操作
 */
public class ElvesDao {
    private final DBHelper dbHelper;

    public ElvesDao(Context context) {
        //创建数据库
        dbHelper = new DBHelper(context);
    }

    /**
     * 添加精灵数据
     */
    public void insertData(int elves_id, String elves_name, int elves_unlocking, int elves_wisdom, String elves_message, int elves_image, int elves_anim_normal, int elves_anim_click, int elves_start, int elves_point) {
        //db对象
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        try {
            //开启事务
            db.beginTransaction();
            //存储体
            ContentValues values = new ContentValues();
            //添加数据
            //精灵编号
            values.put("elves_id", elves_id);
            //精灵名称
            values.put("elves_name", elves_name);
            //精灵解锁状态
            values.put("elves_unlocking", elves_unlocking);
            //精灵智慧状态
            values.put("elves_wisdom", elves_wisdom);
            //精灵信息
            values.put("elves_message", elves_message);
            //精灵图片
            values.put("elves_image", elves_image);
            //精灵常态动画
            values.put("elves_anim_normal", elves_anim_normal);
            //精灵点击喂食动画
            values.put("elves_anim_click", elves_anim_click);
            //精灵启动状态
            values.put("elves_start", elves_start);
            //花费的祈愿点
            values.put("elves_point", elves_point);

            //插入数据
            db.insert("elves", null, values);
            //事务成功
            db.setTransactionSuccessful();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //结束事务
            db.endTransaction();
            //关闭数据库
            db.close();
        }
    }

    /**
     * 所有精灵信息查询，主要用于在图鉴页面显示，解锁状态判断
     */
    public List<ElvesBean> allElvesQuery() {
        //db对象
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        List<ElvesBean> elvesBeanList = new ArrayList<>();
        ElvesBean elvesBean ;
        try {
            //开启事务
            db.beginTransaction();
            String sql = "select * from elves";
            Cursor cursor = db.rawQuery(sql, null);
            while (cursor.moveToNext()) {
                elvesBean = new ElvesBean();
                elvesBean.setElves_id(cursor.getInt(cursor.getColumnIndex("elves_id")));
                elvesBean.setElves_name(cursor.getString(cursor.getColumnIndex("elves_name")));
                elvesBean.setElves_unlocking(cursor.getInt(cursor.getColumnIndex("elves_unlocking")));
                elvesBean.setElves_wisdom(cursor.getInt(cursor.getColumnIndex("elves_wisdom")));
                elvesBean.setElves_message(cursor.getString(cursor.getColumnIndex("elves_message")));
                elvesBean.setElves_image(cursor.getInt(cursor.getColumnIndex("elves_image")));
                elvesBean.setElves_anim_normal(cursor.getInt(cursor.getColumnIndex("elves_anim_normal")));
                elvesBean.setElves_anim_click(cursor.getInt(cursor.getColumnIndex("elves_anim_click")));
                elvesBean.setElves_start(cursor.getInt(cursor.getColumnIndex("elves_start")));
                elvesBean.setElves_point(cursor.getInt(cursor.getColumnIndex("elves_point")));
                elvesBeanList.add(elvesBean);
            }
            cursor.close();
            db.setTransactionSuccessful();
            Log.v("myapp", "查询成功,一共：" + elvesBeanList.size() + "条数据");
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //结束事务
            db.endTransaction();
            //关闭数据库
            db.close();
        }
        return elvesBeanList;
    }

    /**
     * 精灵信息更新，更新解锁状态,根据id,用于解锁精灵图鉴
     */
    public void elvesLockingUpdate(ElvesBean elvesBean) {
        //db对象
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        try {
            //开启事务
            db.beginTransaction();
            String[] args = {String.valueOf(elvesBean.getElves_id())};
            //存储体
            ContentValues values = new ContentValues();
            //精灵解锁状态
            values.put("elves_unlocking", 1);
            //更新数据
            db.update("elves", values, "elves_id = ?", args);
            //事务成功
            db.setTransactionSuccessful();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //结束事务
            db.endTransaction();
            //关闭数据库
            db.close();
        }
    }

    /**
     * 精灵信息更新，更新开启状态，根据id,用于详情页，显示精灵开启或关闭
     */
    public void elvesStartUpdate(ElvesBean elvesBean) {
        //db对象
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        try {
            //开启事务
            db.beginTransaction();
            String[] args = {String.valueOf(elvesBean.getElves_id())};
            //存储体
            ContentValues values = new ContentValues();
            //精灵开启状态
            values.put("elves_start", elvesBean.getElves_start());
            //更新数据
            db.update("elves", values, "elves_id = ?", args);
            //事务成功
            db.setTransactionSuccessful();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //结束事务
            db.endTransaction();
            //关闭数据库
            db.close();
        }
    }
    /**
     * 精灵信息更新，更新关闭状态,用于详情页，
     */
    public void allElvesStartUpdate() {
        //db对象
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        try {
            //开启事务
            db.beginTransaction();
            //存储体
            ContentValues values = new ContentValues();
            //精灵开启状态
            values.put("elves_start", 0);
            //更新数据
            db.update("elves", values, null, null);
            //事务成功
            db.setTransactionSuccessful();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //结束事务
            db.endTransaction();
            //关闭数据库
            db.close();
        }
    }
    /**
     * 精灵信息更新，更新智慧状态，用于智慧中心，开启或关闭
     */
    public void elvesWisdomUpdate(int elves_wisdom) {
        //db对象
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        try {
            //开启事务
            db.beginTransaction();
            //存储体
            ContentValues values = new ContentValues();
            //精灵解锁状态
            values.put("elves_wisdom", elves_wisdom);
            //更新数据
            db.update("elves", values, null, null);
            //事务成功
            db.setTransactionSuccessful();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //结束事务
            db.endTransaction();
            //关闭数据库
            db.close();
        }
    }
}
